//
//  HXScenceMenuVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/29.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXScenceMenuVC.h"
#import "HX_Macro.h"
#import "HXScrenceConditionCell.h"
#import "HXScenceMenuModel.h"
#import "HXCustomVideoVC.h"

@interface HXScenceMenuVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *scenceMenu;
@property (nonatomic, assign) NSInteger index;
@end
@implementation HXScenceMenuVC
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (NSMutableArray *)scenceMenu {
    if (!_scenceMenu) {
        _scenceMenu = [NSMutableArray array];
    }
    return _scenceMenu;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.index = 2;
    self.view.backgroundColor = [UIColor orangeColor];
    self.title = @"场景菜谱";
    [self getNetWorkingScenceMenu];
    [self creatCollectionView];
}
- (void)getNetWorkingScenceMenu {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"page=1&methodName=SceneList&size=20&version=4.40&";
    NSDictionary *dicHeared = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
   [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeared Response:HXJSON Success:^(id result) {
       NSDictionary *results = result;
       NSDictionary *dicData = results[@"data"];
       NSArray *arrData = dicData[@"data"];
       for (NSDictionary *dicAll in arrData) {
           HXScenceMenuModel *scenceMenu = [HXScenceMenuModel ModelWithDic:dicAll];
           [self.scenceMenu addObject:scenceMenu];
           [_collectionView reloadData];
       }
      } Failure:^(NSError *error) {
    }];
}
- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"HXScrenceConditionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"scrence"];
    MJRefreshAutoNormalFooter *foot = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];
    self.collectionView.mj_footer = foot;
}
- (void)footer {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"page=%ld&methodName=SceneList&size=20&version=4.40&", self.index];
    NSDictionary *dicHeared = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeared Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dicAll in arrData) {
            HXScenceMenuModel *scenceMenu = [HXScenceMenuModel ModelWithDic:dicAll];
            [self.scenceMenu addObject:scenceMenu];
            [_collectionView reloadData];
            [self.collectionView.mj_footer endRefreshing];
        }
    } Failure:^(NSError *error) {
    }];
    _index++;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.scenceMenu.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HXScrenceConditionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"scrence" forIndexPath:indexPath];
    cell.backgroundColor = COLORAUTO;
    HXScenceMenuModel *scence = self.scenceMenu[indexPath.row];
    cell.detail.text = scence.scene_title;
    [cell.imgCell sd_setImageWithURL:[NSURL URLWithString:scence.scene_background]];
    cell.countSas.text = [NSString stringWithFormat:@"%ld道菜", scence.scene_id];
    return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HXCustomVideoVC *vc = [[HXCustomVideoVC alloc] init];
    HXScenceMenuModel *scence = self.scenceMenu[indexPath.row];
    vc.ID = scence.scene_id;
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.collectionView.HX_width / 3, self.collectionView.HX_height / 3.6);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
